package com.datastructures2.graph.最小生成树;

/**
 * 带权重的边
 * @author MaoLin Wang
 * @date 2020/2/239:49
 */
public class Edge implements Comparable<Edge>{
    private final int v;
    private final int w;
    private final double weight;

    public Edge(int v, int w, double weight) {
        if (v < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
        if (w < 0) throw new IllegalArgumentException("vertex index must be a nonnegative integer");
        if (Double.isNaN(weight)) throw new IllegalArgumentException("权重必须为数值");
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    /**
     * 返回权重
     * @return
     */
    public double weight() {
        return weight;
    }

    /**
     * 返回边两端的顶点之一
     * @return
     */
    public int either() {
        return v;
    }

    /**
     * 返回另一个顶点
     * @param vertex
     * @return
     */
    public int other(int vertex) {
        if      (vertex == v) return w;
        else if (vertex == w) return v;
        else throw new IllegalArgumentException("Illegal endpoint");
    }
    @Override
    public int compareTo(Edge edge) {
        return Double.compare(this.weight, edge.weight);
    }
    public String toString() {
        return String.format("%d-%d %.5f", v, w, weight);
    }
}
